package BookStore.dao;

import BookStore.model.User;
import BookStore.util.DbUtil;
import BookStore.view.LoginFrame;
import BookStore.view.Register;

import javax.swing.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class UserDao {
    String name;
    String pswd;
    public UserDao(User user) {
        name = user.getUserName();
        pswd = user.getUserPsw();
    }

    public void register() throws Exception {
        Connection con = DbUtil.getCon();
        Statement statement = con.createStatement();
        String sql1 = "select * from jdbc where name = '" + name + "' ";
        ResultSet result = statement.executeQuery(sql1);
        if (result.next()) {
            JOptionPane.showMessageDialog(null, "该用户名已存在");
        } else {
            String sql2 = "insert into jdbc (name ,psw) values ('" + name + "','" + pswd + "')";
            statement.executeUpdate(sql2);
            JOptionPane.showMessageDialog(null, "注册成功");
            new LoginFrame().setVisible(true);
        }
        con.close();
        statement.close();

    }
}

